cmake_minimum_required(VERSION 3.2)

file(GLOB BOLT_CONFIGURE_FILE $ENV{BOLT_ROOT}/bolt.cmake ${BOLT_ROOT}/bolt.cmake)
if (BOLT_CONFIGURE_FILE)
    include(${BOLT_CONFIGURE_FILE})
else (BOLT_CONFIGURE_FILE)
    message(FATAL_ERROR "
FATAL: can not find bolt.cmake in <BOLT_ROOT> directory,
       please set shell or cmake environment variable BOLT_ROOT.
    ")
endif (BOLT_CONFIGURE_FILE)

project(tools)

set_policy()

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${BOLT_ROOT}/cmakes")
find_package(Uni)
find_package(ModelTools)
find_package(Image)
find_package(TensorComputing)
find_package(Inference)
find_package(jpeg)
if(USE_MALI)
    find_package(Gcl)
endif(USE_MALI)

set_project_install_directory()
set_c_cxx_flags()
set_test_c_cxx_flags()

function(tensor_computing name)
    add_executable(${name} ${name}.cpp)
    add_dependencies(${name} tensor_computing)
    add_dependencies(${name} tensor_computing_static)
    target_link_libraries(${name} ${TENSOR_COMPUTING_LIBRARIES})
    if(USE_MALI)
        target_link_libraries(${name} ${OPENCL_LIBRARIES})
    endif(USE_MALI)
endfunction()

function(model_tools name)
    add_executable(${name} ${name}.cpp)
    if (USE_CAFFE)
        add_dependencies(${name} model-tools)
        add_dependencies(${name} model-tools_static)
        add_dependencies(${name} model-tools_caffe)
        add_dependencies(${name} model-tools_caffe_static)
        TARGET_LINK_LIBRARIES(${name} ${MODEL_TOOLS_LIBRARIES})
    endif (USE_CAFFE)

    if (USE_ONNX)
        add_dependencies(${name} model-tools)
        add_dependencies(${name} model-tools_static)
        add_dependencies(${name} model-tools_onnx)
        add_dependencies(${name} model-tools_onnx_static)
        TARGET_LINK_LIBRARIES(${name} ${MODEL_TOOLS_LIBRARIES})
    endif (USE_ONNX)

    if (USE_TFLITE)
        add_dependencies(${name} model-tools)
        add_dependencies(${name} model-tools_static)
        add_dependencies(${name} model-tools_tflite)
        add_dependencies(${name} model-tools_tflite_static)
        TARGET_LINK_LIBRARIES(${name} ${MODEL_TOOLS_LIBRARIES})
    endif (USE_TFLITE)
endfunction()

if (USE_CAFFE)
    model_tools(caffe2bolt)
endif (USE_CAFFE)
if (USE_ONNX)
    model_tools(onnx2bolt)
endif (USE_ONNX)
if (USE_TFLITE)
    model_tools(tflite2bolt)
endif (USE_TFLITE)

if (USE_LIBRARY_TUNING)
    tensor_computing(tensor_computing_library_search)
endif (USE_LIBRARY_TUNING)

if (BUILD_TEST)
    if (USE_INT8)
        inference(ptq_calibration ./ptq_calibration.cpp)
    endif (USE_INT8)
endif (BUILD_TEST)
